﻿using UnityEngine;

namespace Core
{
    public static partial class Utils
    {
        //https://www.jianshu.com/p/df72513fb6cd

        public static class Json
        {
            public static string Serialize<T>(T t)
            {
                string str = JsonUtility.ToJson(t);
                return str;
            }

            public static T Deserialize<T>(string str)
            {
                T data = JsonUtility.FromJson<T>(str);
                return data;
            }

            /// <summary>
            /// 无法Clone复杂结构
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="t"></param>
            /// <returns></returns>
            public static T Clone<T>(T t)
            {
                var json = Serialize(t);
                var data = Deserialize<T>(json);
                return data;
            }
        }
    }
}
